package cc.lucien.dao;

import cc.lucien.entity.User;
import cc.lucien.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private Connection connection;
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;

    /**
     * 登录方法
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from users where username = ? and password = ?";
            preparedStatement = connection.prepareStatement(sql);
            setParams(username, password);
            resultSet = preparedStatement.executeQuery();
            List<User> list = rsToList(resultSet);
            if (list != null && !list.isEmpty()) {
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 注册方法
     * @param user
     */
    @Override
    public void register(User user) {
        try {
            connection = DBUtils.getConnection();
            String sql = "insert into users(name,username,password,tel,email,state) values (?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            Object[] params = {
                    user.getName(),user.getUsername(),user.getPassword(),user.getTel(),user.getEmail(),user.getState()
            };
            setParams(params);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection,preparedStatement,resultSet);
        }
    }

    /**
     * 数据更新方法
     * @param user
     */
    @Override
    public void edit(User user) {
        try {
            connection = DBUtils.getConnection();
            String sql = "update users set name = ?,username = ?,password = ?,tel = ?,email = ?,state = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            Object[] params = {
                    user.getName(), user.getUsername(), user.getPassword(), user.getTel(), user.getEmail(),user.getState(), user.getId()
            };
            setParams(params);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection,preparedStatement,resultSet);
        }
    }

    /**
     * 查询表中所有记录
     * @return
     */
    @Override
    public List<User> getUsers() {
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from users";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            List<User> list = rsToList(resultSet);
            if (list != null) {
                return list;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection,preparedStatement,this.resultSet);
        }
        return null;
    }

    /**
     * 根据id查询数据库中一条记录
     * @param id
     * @return
     */
    @Override
    public User getUser(int id) {
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from users where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            setParams(id);
            resultSet = preparedStatement.executeQuery();
            List<User> list = rsToList(resultSet);
            if (list != null) {
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection,preparedStatement,this.resultSet);
        }
        return null;
    }

    /**
     * 解析结果集，返回JavaBean集合
     * @param resultSet
     * @return
     */
    private List<User> rsToList(ResultSet resultSet) {
        List<User> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setName(resultSet.getString("name"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setEmail(resultSet.getString("email"));
                user.setTel(resultSet.getString("tel"));
                user.setState(resultSet.getInt("state"));
                list.add(user);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(connection,preparedStatement,this.resultSet);
        }
        return null;
    }

    /**
     * 给占位符赋值
     * @param params
     */
    private void setParams(Object... params) {
        if (params == null) {
            return;
        }
        for (int i = 0; i < params.length; i++) {
            try {
                preparedStatement.setObject(i+1,params[i]);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
